package org.shuma.goods.service.impl;

import org.shuma.goods.domain.DTO.ProductDTO;
import org.shuma.goods.domain.entity.ProductDetailsInfo;
import org.shuma.goods.mapper.ProductDetailsInfoMapperByXingWang;
import org.shuma.goods.service.ProductDetailsInfoServiceByXingWang;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * (ProductDetailsInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-06-18 13:50:09
 */
@Service("productDetailsInfoServiceByXingWangImpl")
public class ProductDetailsInfoServiceByXingWangImpl implements ProductDetailsInfoServiceByXingWang {
    /*@SofaReference(interfaceType = UploadFile.class, uniqueId = "${service.oss.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private UploadFile uploadFile;*/
    @Resource
    private ProductDetailsInfoMapperByXingWang productDetailsInfoMapperByXingWang;

    /**
     * 通过ID查询单条数据
     *
     * @param pDetailsId 主键
     * @return 实例对象
     */
    @Override
    public ProductDetailsInfo queryById(Long pDetailsId) {
        return this.productDetailsInfoMapperByXingWang.queryById(pDetailsId);
    }

    /**
     * 分页查询
     *
     * @param productDetailsInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ProductDetailsInfo> queryByPage(ProductDetailsInfo productDetailsInfo, PageRequest pageRequest) {
        long total = this.productDetailsInfoMapperByXingWang.count(productDetailsInfo);
        return new PageImpl<>(this.productDetailsInfoMapperByXingWang.queryAllByLimit(productDetailsInfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据

     * @return 实例对象
     */
    @Override
    public ProductDetailsInfo insert(ProductDTO productDTO,MultipartFile file) {
        ProductDetailsInfo productDetailsInfo = new ProductDetailsInfo();
        BeanUtils.copyProperties(productDTO,productDetailsInfo);
        //String filePath = UUID.randomUUID().toString().replaceAll("-", "");
        //String fileUrl = uploadFile.uploadByInputStream(file,productDTO.getProductName(), filePath);
        String fileUrl = "dabsndba";
        productDetailsInfo.setProductPicture(fileUrl);
        this.productDetailsInfoMapperByXingWang.insert(productDetailsInfo);
        return productDetailsInfo;
    }

    /**
     * 修改数据
     *
     * @param productDetailsInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ProductDetailsInfo update(ProductDetailsInfo productDetailsInfo) {

        this.productDetailsInfoMapperByXingWang.update(productDetailsInfo);
        return this.queryById(productDetailsInfo.getPDetailsId());
    }

    /**
     * 通过主键删除数据
     *
     * @param pDetailsId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long pDetailsId) {
        return this.productDetailsInfoMapperByXingWang.deleteById(pDetailsId) > 0;
    }
}
